﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using DTO;
namespace DAO
{
    public class KhachHangDAO : DataProvider
    {
        protected override object GetDataFromDataRow(DataTable dt, int i)
        {
           

            KhachHangDTO objectDTO = new KhachHangDTO();
            objectDTO.MaKhachHang = int.Parse(dt.Rows[i]["MaKhachHang"].ToString());
            objectDTO.TenKhachHang = dt.Rows[i]["TenKhachHang"].ToString();
            objectDTO.CMND = dt.Rows[i]["CMND"].ToString();
            objectDTO.DiaChi = dt.Rows[i]["DiaChi"].ToString();
            objectDTO.DienThoai = dt.Rows[i]["DienThoai"].ToString();
            objectDTO.Password = dt.Rows[i]["Password"].ToString();
            objectDTO.ThongTinTaiKhoan = dt.Rows[i]["ThongTinTaiKhoan"].ToString();
            objectDTO.Username = dt.Rows[i]["Username"].ToString();
            objectDTO.TrangThai = int.Parse(dt.Rows[i]["TrangThai"].ToString());
            return (object)objectDTO;
        }

        public KhachHangDTO[] GetAll()
        {
            String strSQL = "Select * from KHACHHANG ";
            DataTable dt = new DataProvider().ExecuteQuery(strSQL);

            KhachHangDTO[] khachhangDTOs = new KhachHangDTO[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
                khachhangDTOs[i] = (KhachHangDTO)GetDataFromDataRow(dt, i);

            return khachhangDTOs;
        }

        public bool insert(KhachHangDTO info)
        {
            string insertCommand = "INSERT KHACHHANG(TenKhachHang,CMND,DiaChi,DienThoai,ThongTinTaiKhoan,Username,Password,TrangThai) VALUES(N'" +
                info.TenKhachHang + "', '" +
                info.CMND+ "', N'" +
                info.DiaChi + "', '" +
                info.DienThoai + "', '" +
                info.ThongTinTaiKhoan + "', '" +
                info.Username + "','" +
                info.Password + "'," +
                0 +")";

           return ExecuteNonQuery(insertCommand);
        }

        public void update(KhachHangDTO info)
        {
            string updateCommand = "UPDATE KHACHHANG " +
            "SET TenKhachHang = '" + info.TenKhachHang + "', " +
            " CMND = '" + info.CMND + "', " +
            " DiaChi = '" + info.DiaChi + "', " +
            " DienThoai = '" + info.DienThoai + "'," +
            " ThongTinTaiKhoan = '" + info.ThongTinTaiKhoan + "'," +
            " Username = '" + info.Username + "'," +
            " Password = '" + info.Password + "'" +
            " WHERE MaKhachHang = " + info.MaKhachHang;


            ExecuteNonQuery(updateCommand);
        }

        public void delete(string maKhachHang)
        {
            string deleteCommand = "DELETE FROM KHACHHANG WHERE MaKhachHang = " + maKhachHang;
            ExecuteNonQuery(deleteCommand);
        }

        //Kiem Tra Dang Nhap --- Thai Pham Duy
        public bool CheckLogin(String Username,String Pass)
        {
            KhachHangDTO kh = null;
            Pass = Pass + "Mam&Muoi";
            Pass = Pass.GetHashCode().ToString();
            String strSQL = "Select * from KHACHHANG Where Username='"+Username+"' AND Password='"+Pass+"'";
            DataTable dt = new DataProvider().ExecuteQuery(strSQL);
            for (int i = 0; i < dt.Rows.Count; i++)
                kh = (KhachHangDTO)GetDataFromDataRow(dt, i);
            return kh==null?false:true;
        }

        public int GetIDUser(string username)
        {
            string strSQL = "Select MaKhachHang from KHACHHANG Where Username = '" + username + "'";
            return this.ExcuteScalar(strSQL);
        }
        public bool UpdateTinhTrang(int MaKhachHang)
        {
            string sql = "Update KHACHHANG Set TrangThai = 1 Where MaKhachHang=" + MaKhachHang;
            return this.ExecuteNonQuery(sql);
        }
        public bool UpdateTinhTrang0(int MaKhachHang)
        {
            string sql = "Update KHACHHANG Set TrangThai = 0 Where MaKhachHang=" + MaKhachHang;
            return this.ExecuteNonQuery(sql);
        }
    }
}
